package com.websense.training.books

import java.io.FileOutputStream
import com.itextpdf.text.BaseColor
import com.itextpdf.text.Document
import com.itextpdf.text.Font
import com.itextpdf.text.FontFactory
import com.itextpdf.text.Paragraph
import com.itextpdf.text.pdf.PdfWriter
import com.itextpdf.text.pdf.PdfPCell
import com.itextpdf.text.pdf.PdfPTable
import com.itextpdf.text.Rectangle
import com.itextpdf.text.Element
import com.itextpdf.text.PageSize
import com.itextpdf.text.Chunk

class BuildToc(var pdfOutFile: String) {
  
  def scanLeft[a,b](xs:Iterable[a])(s:b)(f : (b,a) => b) =
  xs.foldLeft(List(s))( (acc,x) => f(acc(0), x) :: acc).reverse
  
  val logger = org.apache.log4j.Logger
    .getLogger(classOf[BuildTextParagraphs])

  def addDots(arg: String): String = {
    var dottedLine = arg
    for (i <- 0 to 100) {
      val ch = new Chunk(dottedLine, FontFactory
        .getFont(FontFactory.HELVETICA, 14,
          Font.NORMAL, BaseColor.BLACK))
      //        Console.out.println("widthPoint is " + ch.getWidthPoint())
      if (ch.getWidthPoint() > 390.0f) {
        return dottedLine
      }
      dottedLine += " .";
    }
    dottedLine
  }

  def processStatic(staticData: List[String], pageCounts: List[Int]): Unit = {
    val document = new Document();

    document.setPageSize(PageSize.LETTER);
    document.setMargins(36, 36, 54, 72)

    PdfWriter.getInstance(document,
      new FileOutputStream(pdfOutFile))

    document.open();

    val table = new PdfPTable(2)
    table.setWidthPercentage(81.0f)
    table.setWidths(Array(555, 45))

    val p1 = new Paragraph("Table of Contents",
      FontFactory
        .getFont(FontFactory.HELVETICA_BOLD, 16,
          Font.NORMAL, BaseColor.BLACK))
    p1.setIndentationLeft(50.0f)
    p1.setLeading(20.0f, 1.0f)
    val cell1 = new PdfPCell(p1)
    cell1.setColspan(2)
    cell1.setBorderColor(new BaseColor(12, 33, 115))
    cell1.setBorderWidth(1.4f)
    cell1.setBorder(Rectangle.BOTTOM)
    cell1.setBorder(Rectangle.TOP)
    cell1.setHorizontalAlignment(Element.ALIGN_LEFT)
    cell1.setMinimumHeight(50.0f)
    cell1.setPaddingBottom(10.0f)
    cell1.setVerticalAlignment(Element.ALIGN_BOTTOM)
    table.addCell(cell1);

    val p0 = new Paragraph(" ")
    val c0 = new PdfPCell(p0)
    c0.setBorder(Rectangle.TOP)
    c0.setBorderWidth(1.4f)
    c0.setBorderColor(new BaseColor(12, 33, 115))
    c0.setMinimumHeight(20.0f)
    table.addCell(c0)
    table.addCell(c0)
    
    // The first 6 pages are used for cover, copyright and TOC
    val pageNums = scanLeft(pageCounts)(7)(_+_)
    for (idx <- 0 until staticData.length) {

      val dottedLine = addDots(staticData(idx))
      val p2 = new Paragraph(dottedLine,
        FontFactory
          .getFont(FontFactory.HELVETICA, 14,
            Font.NORMAL, BaseColor.BLACK))

      val cell2 = new PdfPCell(p2)
      cell2.setHorizontalAlignment(Element.ALIGN_JUSTIFIED_ALL)
      cell2.setMinimumHeight(35.0f)
      cell2.setBorder(Rectangle.NO_BORDER)
      table.addCell(cell2)

      val p3 = new Paragraph("" + pageNums(idx),
        FontFactory
          .getFont(FontFactory.HELVETICA, 14,
            Font.NORMAL, BaseColor.BLACK))
      p3.setAlignment(Element.ALIGN_RIGHT)
      val cell3 = new PdfPCell(p3)
      cell3.setHorizontalAlignment(Element.ALIGN_RIGHT)
      cell3.setMinimumHeight(35.0f)
      cell3.setBorder(Rectangle.NO_BORDER)

      table.addCell(cell3)
    }

    document.add(table)

    document.close();

  }

}